import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Django.settings")
import django
django.setup()
from django.shortcuts import render, redirect,HttpResponse, render_to_response
from salesSum.models import *
from django.db.models import Sum

# Create your views here.

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


def index(request):
    sum1 = Salesrecord.objects.filter(supermarket_id="0001").aggregate(sum=Sum('amount'))
    sum2 = Salesrecord.objects.filter(supermarket_id="0002").aggregate(sum=Sum('amount'))
    sum3 = Salesrecord.objects.filter(supermarket_id="0003").aggregate(sum=Sum('amount'))

    if sum1['sum']:
        Salessum.objects.filter(id=1).update(sum=sum1['sum'])
    if sum2['sum']:
        Salessum.objects.filter(id=2).update(sum=sum2['sum'])
    if sum3['sum']:
        Salessum.objects.filter(id=3).update(sum=sum3['sum'])
    sum_list = Salessum.objects.all()
    return render(request, 'sale_sum.html', {'sum_list': sum_list})